package com.lcz.audioplayer.view;

import android.content.Context;
import android.util.Log;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 作者:     刘传政
 * 创建时间:  10:16 2024/4/23 0023
 * QQ:      1052374416
 * 电话:     18501231486
 * 描述:
 * [ti:歌曲名]
 * [ar:歌手名]
 * [al:专辑名]
 * [by:编辑者]
 * [offset:时间补偿值]
 * [00:00.00]或[00:00.000]
 */
public class LrcUtil {
    static String TAG = LrcUtil.class.getSimpleName();

    public static List<LrcBean> parseLrcFromAssets(Context context, String fileName) {
        try {
            return parseInputStream(context.getResources().getAssets().open(fileName));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static List<LrcBean> parseLrcFromFile(Context context, File file) {
        try {
            return parseInputStream(new FileInputStream(file));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    private static List<LrcBean> parseInputStream(InputStream inputStream) {
        List<LrcBean> lrcs = new ArrayList<>();
        InputStreamReader isr = new InputStreamReader(inputStream);
        BufferedReader bufferedReader = new BufferedReader(isr);
        try {
            String line;

            while ((line = bufferedReader.readLine()) != null) {
                Log.i(TAG, line);
                List<LrcBean> lineList = parseLine(line);
                if (lineList != null && lineList.size() != 0) {
                    lrcs.addAll(lineList);
                }
            }
            sortLrcs(lrcs);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return lrcs;
    }
    private static void sortLrcs(List<LrcBean> lrcs) {
        Collections.sort(lrcs, new Comparator<LrcBean>() {
            @Override
            public int compare(LrcBean o1, LrcBean o2) {
                if (o1.time < o2.time) {
                    return -1;
                }else if (o1.time == o2.time){
                    return 0;
                }else {
                    return 1;
                }

            }
        });
    }
    //[03:56.00][03:18.00][02:06.00][01:07.00]原谅我这一生不羁放纵爱自由
    private static final String LINE_REGEX = "((\\[\\d{2}:\\d{2}\\.\\d{2}])+)(.*)";
    private static final String TIME_REGEX = "\\[(\\d{2}):(\\d{2})\\.(\\d{2})]";

    /**
     * 解析一行文本，因为可能一行对应多行，所以返回list
     * [03:15.71][01:13.75]陪我到地老天荒
     *
     * @param line
     * @return
     */
    private static List<LrcBean> parseLine(String line) {
        if (line == null || line.trim().isEmpty()) {
            return null;
        }
        List<LrcBean> lrcList = new ArrayList<>();
        Matcher matcher = Pattern.compile(LINE_REGEX).matcher(line);
        if (!matcher.matches()) {
            return null;
        }
        String times = matcher.group(1);
        String content = matcher.group(3);
        Matcher timeMatcher = Pattern.compile(TIME_REGEX).matcher(times);
        while (timeMatcher.find()) {
            String min = timeMatcher.group(1);
            String sec = timeMatcher.group(2);
            String mil = timeMatcher.group(3);
            LrcBean lrc = new LrcBean();
            if (content != null && content.length() != 0) {
                int scale_mil = Long.parseLong(mil) > 100 ? 1 : 10;//如果毫秒是3位数则乘以1，反正则乘以10
                lrc.time = Long.parseLong(min) * 60 * 1000 + Long.parseLong(sec) * 1000 + Long.parseLong(mil) * scale_mil;
                lrc.text = content;
                lrcList.add(lrc);//得到一行歌词
            }
        }
        return lrcList;
    }
}
